//
//  DisplayCell.m
//  CoreDataDemo
//
//  Created by macbook on 12/26/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "DisplayCellView.h"
//#import "MySong.h"
//#import "LyricViewerItem.h"
#import "Song.h"
#import "Musician.h"
#import "QuartzCore/CAlayer.h"
//#import "CharacterProcess.h"

@implementation DisplayCellView
@synthesize btnSelect, btnMenu;
@synthesize imgFavourite, imgRemix, imgSinger;
@synthesize lbSongName, lbAuthor, lbLyrics, lbSongCode;
@synthesize indexPath;
@synthesize delegate;
@synthesize tableView;

//- (IBAction)KaraokeFavouriteDidPressed:(id)sender {
//    if(delegate && [delegate respondsToSelector:@selector(karaokeCell:didSelectFavouriteAtIndexPath:)])
//        [delegate karaokeCell:self didSelectFavouriteAtIndexPath:indexPath];
//}
//
//- (IBAction)buttonViewLyricDidPressed:(id)sender {
//    if(delegate && [delegate respondsToSelector:@selector(karaokeCell:didSelectViewLyricAtIndexPath:)])
//        [delegate karaokeCell:self didSelectViewLyricAtIndexPath:indexPath];
//}
//
//- (IBAction)buttonFirstReserveDidPressed:(id)sender {
//    if(delegate && [delegate respondsToSelector:@selector(karaokeCell:didSelectFirstReserveAtIndexPath:)])
//        [delegate karaokeCell:self didSelectFirstReserveAtIndexPath:indexPath];
//}

- (IBAction)karaokeFunctionDidPressed:(id)sender {
    if(tableView != nil && delegate != nil && [delegate respondsToSelector:@selector(tableView:didSelectOnFunctionAtIndexPath:)])
    {
        [delegate tableView:tableView didSelectOnFunctionAtIndexPath:self.indexPath];
    }
}

- (IBAction)menuItemDidPressed:(id)sender {
    if(tableView != nil && delegate != nil && [delegate respondsToSelector:@selector(tableView:didSelectOnMenuItemAtIndexPath:)])
    {
        [delegate tableView:tableView didSelectOnMenuItemAtIndexPath:self.indexPath];
    }
}

- (DisplayCellView *) configureCellForSongObject: (Song *)song atIndexPath:(NSIndexPath *)_indexPath withSearchString: (NSString *) search andShowFree:(BOOL)isFree  inPlaylist:(BOOL) seleted
{
//    NSInteger songCode = song.id;
    self.lbSongCode.text = [NSString stringWithFormat:@"%05d", song.id];
    [self.lbSongCode setTextColor:KOK_COLOR_NUMBER];
    
    NSString *name = song.name;
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:name];
    NSRange searchRange = [song.titleRaw rangeOfString:search options:NSCaseInsensitiveSearch];
    if (searchRange.location == NSNotFound) {
        searchRange = [song.shortname rangeOfString:search options:NSCaseInsensitiveSearch];
        if (searchRange.location != NSNotFound) {
            NSArray * strArray = [song.titleRaw componentsSeparatedByString:@" "];
            NSInteger pos = 0;
            for(NSInteger i= 0; i < searchRange.location; i++)
            {
                pos += [[strArray objectAtIndex:i] length] + 1;
            }
            
            for (NSInteger i = searchRange.location; i < searchRange.length; i++) {
                [attrString addAttribute:NSForegroundColorAttributeName value:KOK_COLOR_SEARCH_HIGHLIGHT range:NSMakeRange(pos, 1)];
                
                pos += [[strArray objectAtIndex:i] length] + 1;
            }
        }else {
            searchRange = [name rangeOfString:search options:NSCaseInsensitiveSearch];
            if(searchRange.location != NSNotFound) {
                [attrString addAttribute:NSForegroundColorAttributeName value:KOK_COLOR_SEARCH_HIGHLIGHT range:searchRange];
            }
        }
    }else {
        [attrString addAttribute:NSForegroundColorAttributeName value:KOK_COLOR_SEARCH_HIGHLIGHT range:searchRange];
    }
    [self.lbSongName setTextColor:KOK_COLOR_TITLE];
    self.lbSongName.attributedText = attrString;
    
    [self.lbLyrics setTextColor:KOK_COLOR_LYRIC];
    self.lbLyrics.text = song.lyric;
    
    [self.lbAuthor setTextColor:KOK_COLOR_AUTHOR];
    [self.lbAuthor setText:song.musicianName];
    
    /*
    if ([song isSinger]) {
        imgSinger.image = [UIImage imageNamed:@"song-singer.png"];
    }else{
        imgSinger.image = nil;
    }
    */
    
    if([song isNewSong]) {
        imgRemix.image = [UIImage imageNamed:@"ic_new.png"];
    }else {
        imgRemix.image = nil;
    }
    
    if (song.favourite == 1) {
        imgFavourite.image = [UIImage imageNamed:@"ic_like.png"];
    }else {
        imgFavourite.image = nil; 
    }
    
    if ([song isHotSong]) {
        imgSinger.image = [UIImage imageNamed:@"ic_hot.png"];
    }else {
        imgSinger.image = nil;
    }
    /*
    if (seleted) {
        [btnSelect setImage:[UIImage imageNamed:@"song-selected.png"] forState:UIControlStateNormal];
        [self.contentView setBackgroundColor:[UIColor whiteColor]];
    } else if (isFree == YES) {
            [btnSelect setImage:[UIImage imageNamed:@"song-func.png"] forState:UIControlStateNormal];
        [self.contentView setBackgroundColor:[UIColor whiteColor]];
    }else {
        [btnSelect setImage:[UIImage imageNamed:@"song-lock.png"] forState:UIControlStateNormal];
        [self.contentView setBackgroundColor:KOK_COLOR_DISABLE];
    }
    */
    return  self;
}

- (DisplayCellView *) configureCellForSongObjectInPlaylist: (Song *)song atIndexPath:(NSIndexPath *)_indexPath withSearchString: (NSString *) search andShowFree:(BOOL)isFree  inPlaylist:(BOOL) seleted
{
    [self configureCellForSongObject:song atIndexPath:indexPath withSearchString:search andShowFree:isFree inPlaylist:seleted];
    if (seleted) {
        [btnSelect setImage:[UIImage imageNamed:@"song-unplaylist.png"] forState:UIControlStateNormal];
    }
    return self; 
}
@end
